package com.music.music.service.impl;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import com.music.common.core.constant.Constants;
import com.music.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.music.music.mapper.CarouselMapper;
import com.music.music.domain.Carousel;
import com.music.music.service.ICarouselService;

import javax.annotation.Resource;

/**
 * 轮播图Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-30
 */
@Service
public class CarouselServiceImpl implements ICarouselService {
    private final ReentrantLock lock = new ReentrantLock();
    @Autowired
    private CarouselMapper carouselMapper;
    @Resource
    RedisService redisService;

    /**
     * 查询轮播图
     *
     * @param id 轮播图主键
     * @return 轮播图
     */
    @Override
    public Carousel selectCarouselById(Integer id) {
        return carouselMapper.selectCarouselById(id);
    }

    /**
     * 查询轮播图列表
     *
     * @param carousel 轮播图
     * @return 轮播图
     */
    @Override
    public List<Carousel> selectCarouselList(Carousel carousel) {
        return carouselMapper.selectCarouselList(carousel);
    }

    public List<Carousel> selectCarouselListWeb(Carousel carousel) {

        // 取redis
        List<Carousel> carouselList = redisService.getCacheList(Constants.CAROUSEL_LIST_KEY);

        if (carouselList == null || carouselList.isEmpty()) {
            lock.lock();
            try {
                // 取redis
                carouselList = redisService.getCacheList(Constants.CAROUSEL_LIST_KEY);
                if (carouselList == null || carouselList.isEmpty()) {
                    carouselList = carouselMapper.selectCarouselList(carousel);

                    // 存redis
                    redisService.setCacheList(Constants.CAROUSEL_LIST_KEY, carouselList);

                    // 设置随机过期时间
                    int randomExpiration = ThreadLocalRandom.current().nextInt(5, 16);
                    redisService.expire(Constants.CAROUSEL_LIST_KEY, randomExpiration, TimeUnit.MINUTES);
                }
            } finally {
                lock.unlock();
            }
        }

        return carouselList;
    }

    /**
     * 新增轮播图
     *
     * @param carousel 轮播图
     * @return 结果
     */
    @Override
    public int insertCarousel(Carousel carousel) {
        return carouselMapper.insertCarousel(carousel);
    }

    /**
     * 修改轮播图
     *
     * @param carousel 轮播图
     * @return 结果
     */
    @Override
    public int updateCarousel(Carousel carousel) {
        return carouselMapper.updateCarousel(carousel);
    }

    /**
     * 批量删除轮播图
     *
     * @param ids 需要删除的轮播图主键
     * @return 结果
     */
    @Override
    public int deleteCarouselByIds(Integer[] ids) {
        return carouselMapper.deleteCarouselByIds(ids);
    }

    /**
     * 删除轮播图信息
     *
     * @param id 轮播图主键
     * @return 结果
     */
    @Override
    public int deleteCarouselById(Integer id) {
        return carouselMapper.deleteCarouselById(id);
    }
}
